package com.masterclient.app.modules.order;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.eoner.baselibrary.utils.Utils;
import com.masterclient.app.R;
import com.eoner.baselibrary.bean.order.PaymentBean;
import com.eoner.baselibrary.utils.ScreenUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者：sean
 * 创建时间：2017/7/25 13:54
 * 功能说明：
 */

public class PaymentMethodDialog extends Dialog {


    private ViewHolder holder;
    private String pay_money;
    PaymentBean.DataBean dataBean;
    private boolean zhbUse = true,wxUse = false;
    private Context context;
    private String fromType;

    public PaymentMethodDialog(@NonNull Context context, PaymentBean.DataBean dataBean) {
        super(context,R.style.MyDialog);
        this.context = context;
        this.pay_money = pay_money;
        this.dataBean = dataBean;
    }


    public PaymentMethodDialog(@NonNull Context context, PaymentBean.DataBean dataBean,String fromType) {
        this(context,dataBean);
        this.fromType = fromType;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_pay_method);
        holder = new ViewHolder(getWindow().getDecorView());
        if ("purchase".equals(fromType)||"sale".equals(fromType)){
            holder.tvPayNow.setBackgroundResource(R.drawable.shape_round_ff6809);
            holder.tvPriceUnit.setTextColor(Color.parseColor("#FF5000"));
            holder.tvPriceSum.setTextColor(Color.parseColor("#FF5000"));
            holder.checkBoxWx.setButtonDrawable(R.drawable.selector_purchase_radio);
            holder.checkBoxZfb.setButtonDrawable(R.drawable.selector_purchase_radio);
        }
        Utils.setDin(holder.tvPriceUnit,context);
        holder.tvPriceSum.setText(dataBean.getSh_should_pay_total());
//        Utils.set_price_style( holder.tvPriceSum,/*"¥ " +*/ dataBean.getSh_should_pay_total());
        holder.checkBoxZfb.setChecked(true);

        if(dataBean.getSh_payment_module()!=null&&dataBean.getSh_payment_module().size()>0){
            List<PaymentBean.DataBean.ShPaymentModuleBean> sh_payment_module = dataBean.getSh_payment_module();
            for(PaymentBean.DataBean.ShPaymentModuleBean paymentModulesBean:sh_payment_module){
                if("1".equals(paymentModulesBean.getSh_id())){
                    zhbUse = true;
                    holder.rlAlipay.setVisibility(View.VISIBLE);
                }

                if("2".equals(paymentModulesBean.getSh_id())){
                    wxUse = true;
                    holder.wxPay.setVisibility(View.VISIBLE);
                }
            }
            if(sh_payment_module.size()==2){
                holder.viewPayLine.setVisibility(View.VISIBLE);
                holder.checkBoxZfb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked) {
                            holder.checkBoxWx.setChecked(false);
                        }
                    }
                });

                holder.checkBoxWx.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked) {
                            holder.checkBoxZfb.setChecked(false);
                        }
                    }
                });
            }else if(sh_payment_module.size()==1){
                holder.viewPayLine.setVisibility(View.GONE);
            }

        }

        holder.checkBoxZfb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    holder.checkBoxWx.setChecked(false);
                }
            }
        });

        holder.checkBoxWx.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    holder.checkBoxZfb.setChecked(false);
                }
            }
        });

        holder.tvPayNow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if((zhbUse&&holder.checkBoxZfb.isChecked())||(wxUse&&holder.checkBoxWx.isChecked())){
                    if(holder.checkBoxZfb.isChecked()){
                        onPayListener.zfbPrePay("1");
                    }else if(holder.checkBoxWx.isChecked()){
                        onPayListener.wxPrePay("2");
                    }

                }else {
                    showToast("请选择一种付款方式");
                }
            }
        });

        holder.rlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    private OnPayListener onPayListener;
    public void setOnPayListener(OnPayListener onPayListener) {
        this.onPayListener = onPayListener;
    }
    public interface OnPayListener{
        public void wxPrePay(String id);
        public void zfbPrePay(String id);
    }


    @Override
    protected void onStart() {
        super.onStart();
        Window window = getWindow();
        window.setWindowAnimations(R.style.bottomShow);
        WindowManager.LayoutParams windowparams = window.getAttributes();
        window.setGravity(Gravity.BOTTOM);
        windowparams.width = ScreenUtils.getScreenWidth();
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setAttributes(windowparams);
    }

    static class ViewHolder {
        @BindView(R.id.rl_back)
        RelativeLayout rlBack;
        @BindView(R.id.checkBox_zfb)
        CheckBox checkBoxZfb;
        @BindView(R.id.checkBox_wx)
        CheckBox checkBoxWx;
        @BindView(R.id.tv_price_unit)
        TextView tvPriceUnit;
        @BindView(R.id.tv_price_sum)
        TextView tvPriceSum;
        @BindView(R.id.tv_pay_now)
        TextView tvPayNow;
        @BindView(R.id.rl_alipay)
                RelativeLayout rlAlipay;
        @BindView(R.id.rl_wxpay)
                RelativeLayout wxPay;
        @BindView(R.id.view_pay_line)
                View viewPayLine;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public void showToast(String str){
        Toast.makeText(context,str,Toast.LENGTH_SHORT).show();
    }
}
